<?php

$databasename = addslashes(trim($_POST["databasename"]));
$usernamedb = addslashes(trim($_POST["usernamedb"]));
$password2 = addslashes(trim($_POST["passworddb"]));
$host = addslashes(trim($_POST["host"]));
$username = addslashes(trim($_POST["username"]));
$password = addslashes(trim($_POST["password"]));
$connect = new mysqli($host, $usernamedb, $password2, $databasename);
if ($connect->connect_error) {
    echo "Lỗi kết nối";
    die;
} else {
    $pass = md5($password);
    $passbase = str_replace("=", "", base64_encode($pass));
    $hashAndSalt = password_hash($passbase, PASSWORD_BCRYPT);
    $sql = "INSERT INTO `tbladmin`(`adminUsername`,`adminpassword`) VALUES('$username','$hashAndSalt');";
    if ($connect->query($sql) === TRUE) {
        $dirfile = str_replace("\\", "/", str_replace("modulesetup", "", __DIR__));
        $file = $dirfile . "index.php";
        $configuse = file($file);
        $flag = 0;
        $configtext = "";
        foreach ($configuse as $line => $linetext) {
            if ($flag == 1) {
                $configtext.="";
            } else {
                $configtext.=$linetext;
            }
            if (strlen(strstr($linetext, "//dieuhuong")) > 0) {
                $flag = 1;
            }if (strlen(strstr($linetext, "//enddieuhuong")) > 0) {
                $flag = 0;
            }
        }
        $filew = fopen($file, "w");
        fwrite($filew, $configtext);
        fclose($filew);

        echo "";
        die;
    } else {
        echo "Không thể khởi tạo.";
        die;
    }
}
$connect->close();
